package org.example.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.example.domain.CarClient;
import org.example.service.CarClientService;

import java.util.List;

public class ClientListener implements ReadListener<CarClient> {

    private static final int BACH_COUNT=3;
    private List<CarClient> cacheList = ListUtils.newArrayListWithExpectedSize(BACH_COUNT);
    private CarClientService carClientService;
    public ClientListener(CarClientService carClientService) {
        this.carClientService=carClientService;
    }

    @Override
    public void invoke(CarClient carClient, AnalysisContext analysisContext) {
        System.err.println("解析出的数据："+carClient);
        cacheList.add(carClient);
        if(cacheList.size()>0 && cacheList!=null ){
            carClientService.insert(cacheList);
            //清空
            cacheList = ListUtils.newArrayListWithExpectedSize(BACH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        System.err.println("解析完成");
        if(cacheList.size()>0){
            carClientService.insert(cacheList);
        }
    }
}
